<?php

class mywallet extends cms_controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('wallet_model', 'wallete');
    }

    public function index($limit = FALSE, $order = FALSE) {
        $this->pass = $this->wallete->filter();
        $this->themes->render('mywallet/index', $this->pass);
    }

    public function add() {
        $data['head_title'] = lang('label_wallet_add_amount');
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('amount', lang('label_wallet_amount'), 'required|trim|is_natural_no_zero');
            $this->form_validation->set_rules('bank_name', lang('label_wallet_bank'), 'required|trim');
            $this->form_validation->set_rules('sender_name', lang('label_wallet_sender_name'), 'required|trim');
            $this->form_validation->set_rules('account_number', lang('label_wallet_account_number'), 'required|trim');
            $this->form_validation->set_rules('note', lang('label_wallet_note'), 'trim');
            if ($this->form_validation->run()) {
                $sql_data = array(
                    'amount' => $this->input->post('amount'),
                    'bank_name' => $this->input->post('bank_name'),
                    'sender_name' => $this->input->post('sender_name'),
                    'account_number' => $this->input->post('account_number'),
                    'note' => $this->input->post('note'),
                    'status' => 'wait',
                    'operation' => 'plus',
                    'user_id' => $this->user_id
                );
                if ($this->wallete->save($sql_data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => 'mywallet/', 'type' => 'success', 'message' => 'message_wallet_note_add_account_wait'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render('mywallet/add', $data);
        }
    }

    public function withdraw() {
        $data['head_title'] = lang('label_wallet_add_amount');
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('amount', lang('label_wallet_amount'), 'required|trim|is_natural_no_zero|callback_max_than_account');
            $this->form_validation->set_rules('bank_name', lang('label_wallet_bank'), 'required|trim');
            $this->form_validation->set_rules('sender_name', lang('label_wallet_sender_name'), 'required|trim');
            $this->form_validation->set_rules('account_number', lang('label_wallet_account_number'), 'required|trim');
            $this->form_validation->set_rules('note', lang('label_wallet_note'), 'trim');
            if ($this->form_validation->run()) {
                $sql_data = array(
                    'amount' => $this->input->post('amount'),
                    'bank_name' => $this->input->post('bank_name'),
                    'sender_name' => $this->input->post('sender_name'),
                    'account_number' => $this->input->post('account_number'),
                    'note' => $this->input->post('note'),
                    'status' => 'wait',
                    'operation' => 'minus',
                    'user_id' => $this->user_id
                );
                if ($this->wallete->save($sql_data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => 'mywallet/', 'type' => 'success', 'message' => 'message_wallet_note_add_account_wait'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render('mywallet/withdraw', $data);
        }
    }

    public function max_than_account($str) {
        $account_wallet = account_wallet();
        if ($str > $account_wallet) {
            $this->form_validation->set_message('max_than_account', lang('message_wallet_account_max_wallet'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

    
}

?>
